<?php

/**
 * This is the model class for table "xman_skill".
 *
 * The followings are the available columns in table 'xman_skill':
 * @property integer $skill_id
 * @property string $skill_name
 * @property integer $skill_categories_id
 *
 * The followings are the available model relations:
 * @property SkillCategories $skillCategories
 * @property UserSkill[] $userSkills
 */
class Skill extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Skill the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_skill';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('skill_name, skill_categories_id', 'required'),
			array('skill_categories_id', 'numerical', 'integerOnly'=>true),
			array('skill_name', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('skill_id, skill_name, skill_categories_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'skillCategories' => array(self::BELONGS_TO, 'SkillCategories', 'skill_categories_id'),
			'userSkills' => array(self::HAS_MANY, 'UserSkill', 'user_skill_skill_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'skill_id' => 'Skill',
			'skill_name' => 'Skill Name',
			'skill_categories_id' => 'Skill Categories',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('skill_id',$this->skill_id);
		$criteria->compare('skill_name',$this->skill_name,true);
		$criteria->compare('skill_categories_id',$this->skill_categories_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * @return array skill level
	 */
	
	public static function listSkillLevel() {
		return array(
			NULL => '---Chọn trình độ---',
			1    => 'Sơ cấp',
			2    => 'Trung cấp',
			3    => 'Cao cấp',
		);
	}
	
	/**
	 * @return array list skill with given category name
	 */
	public static function listSkillName($categoryName) {
		$skillCate = SkillCategories::model()->findByAttributes(array('skill_categories_name'=>$categoryName));
		$skills = $skillCate->skills;
		
		$listName[NULL] = "---Tên kỹ năng---";
		foreach ($skills as $skill) {
			$listName[$skill->skill_id] = $skill->skill_name;
		}
		return $listName;
	}
	
	/**
	 * 
	 */
	
	public static function listAllSkill() {
		$skills     = Skill::model()->findAll();
    	$result[NULL] = "---Chọn kỹ năng---";
    	foreach ($skills as $skill) {
    		$result[$skill->skill_id] = $skill->skill_name;
    	}
    	return $result;
	}
	
	/**
	 * @return array list skill with given name
	 */
	public static function listSkillProgram() {
		$skillProgramCate = SkillCategories::model()->findByAttributes(array('skill_categories_name'=>'program'));
		$skillCates = $skillProgramCate->skillCategories;
		
		$listName = array();
		foreach ($skillCates as $skillCate) {
			$skills = $skillCate->skills;
			foreach ($skills as $skill) {
				$listName[$skill->skill_id] = $skill->skill_name;
			}
		}
		return $listName;
	}
	
	/**
	 * @return array list skill
	 */
	public static function listSkillId($category) {
		$skillCate = SkillCategories::model()->findByAttributes(array('skill_categories_name'=>$category));
		$skills = $skillCate->skills;
		
		$listSkill = array();
		foreach ($skills as $skill) {
			$listSkill[] = $skill->skill_id;
		}
		return $listSkill;
	}
	
	/**
	 * @return array list programming skill
	 */
	public static function listSkillProgramId() {
		$skillProgramCate = SkillCategories::model()->findByAttributes(array('skill_categories_name'=>'program'));
		$skillCates = $skillProgramCate->skillCategories;
		
		$listId = array();
		foreach ($skillCates as $skillCate) {
			$skills = $skillCate->skills;
			foreach ($skills as $skill) {
				$listId[] = $skill->skill_id;
			}
		}
		return $listId;
	}
}